package com.room.search.web.rest;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.room.search.IntegrationTest;
import com.room.search.domain.RoomUser;
import com.room.search.repository.RoomUserRepository;
import com.room.search.service.dto.RoomUserDTO;
import com.room.search.service.mapper.RoomUserMapper;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link RoomUserResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class RoomUserResourceIT {

    private static final String DEFAULT_NICK_NAME = "AAAAAAAAAA";
    private static final String UPDATED_NICK_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_ACCOUNT = "AAAAAAAAAA";
    private static final String UPDATED_ACCOUNT = "BBBBBBBBBB";

    private static final String DEFAULT_PASSWORD = "AAAAAAAAAA";
    private static final String UPDATED_PASSWORD = "BBBBBBBBBB";

    private static final String DEFAULT_MOBILE = "AAAAAAAAAA";
    private static final String UPDATED_MOBILE = "BBBBBBBBBB";

    private static final String DEFAULT_AVATAR = "AAAAAAAAAA";
    private static final String UPDATED_AVATAR = "BBBBBBBBBB";

    private static final String DEFAULT_SLOGAN = "AAAAAAAAAA";
    private static final String UPDATED_SLOGAN = "BBBBBBBBBB";

    private static final String ENTITY_API_URL = "/api/room-users";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));

    @Autowired
    private RoomUserRepository roomUserRepository;

    @Autowired
    private RoomUserMapper roomUserMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restRoomUserMockMvc;

    private RoomUser roomUser;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomUser createEntity(EntityManager em) {
        RoomUser roomUser = new RoomUser()
            .nickName(DEFAULT_NICK_NAME)
            .account(DEFAULT_ACCOUNT)
            .password(DEFAULT_PASSWORD)
            .mobile(DEFAULT_MOBILE)
            .avatar(DEFAULT_AVATAR)
            .slogan(DEFAULT_SLOGAN);
        return roomUser;
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static RoomUser createUpdatedEntity(EntityManager em) {
        RoomUser roomUser = new RoomUser()
            .nickName(UPDATED_NICK_NAME)
            .account(UPDATED_ACCOUNT)
            .password(UPDATED_PASSWORD)
            .mobile(UPDATED_MOBILE)
            .avatar(UPDATED_AVATAR)
            .slogan(UPDATED_SLOGAN);
        return roomUser;
    }

    @BeforeEach
    public void initTest() {
        roomUser = createEntity(em);
    }

    @Test
    @Transactional
    void createRoomUser() throws Exception {
        int databaseSizeBeforeCreate = roomUserRepository.findAll().size();
        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);
        restRoomUserMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomUserDTO)))
            .andExpect(status().isCreated());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeCreate + 1);
        RoomUser testRoomUser = roomUserList.get(roomUserList.size() - 1);
        assertThat(testRoomUser.getNickName()).isEqualTo(DEFAULT_NICK_NAME);
        assertThat(testRoomUser.getAccount()).isEqualTo(DEFAULT_ACCOUNT);
        assertThat(testRoomUser.getPassword()).isEqualTo(DEFAULT_PASSWORD);
        assertThat(testRoomUser.getMobile()).isEqualTo(DEFAULT_MOBILE);
        assertThat(testRoomUser.getAvatar()).isEqualTo(DEFAULT_AVATAR);
        assertThat(testRoomUser.getSlogan()).isEqualTo(DEFAULT_SLOGAN);
    }

    @Test
    @Transactional
    void createRoomUserWithExistingId() throws Exception {
        // Create the RoomUser with an existing ID
        roomUser.setId(1L);
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        int databaseSizeBeforeCreate = roomUserRepository.findAll().size();

        // An entity with an existing ID cannot be created, so this API call must fail
        restRoomUserMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomUserDTO)))
            .andExpect(status().isBadRequest());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void checkAccountIsRequired() throws Exception {
        int databaseSizeBeforeTest = roomUserRepository.findAll().size();
        // set the field null
        roomUser.setAccount(null);

        // Create the RoomUser, which fails.
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        restRoomUserMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomUserDTO)))
            .andExpect(status().isBadRequest());

        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkPasswordIsRequired() throws Exception {
        int databaseSizeBeforeTest = roomUserRepository.findAll().size();
        // set the field null
        roomUser.setPassword(null);

        // Create the RoomUser, which fails.
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        restRoomUserMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomUserDTO)))
            .andExpect(status().isBadRequest());

        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllRoomUsers() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        // Get all the roomUserList
        restRoomUserMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(roomUser.getId().intValue())))
            .andExpect(jsonPath("$.[*].nickName").value(hasItem(DEFAULT_NICK_NAME)))
            .andExpect(jsonPath("$.[*].account").value(hasItem(DEFAULT_ACCOUNT)))
            .andExpect(jsonPath("$.[*].password").value(hasItem(DEFAULT_PASSWORD)))
            .andExpect(jsonPath("$.[*].mobile").value(hasItem(DEFAULT_MOBILE)))
            .andExpect(jsonPath("$.[*].avatar").value(hasItem(DEFAULT_AVATAR)))
            .andExpect(jsonPath("$.[*].slogan").value(hasItem(DEFAULT_SLOGAN)));
    }

    @Test
    @Transactional
    void getRoomUser() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        // Get the roomUser
        restRoomUserMockMvc
            .perform(get(ENTITY_API_URL_ID, roomUser.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(roomUser.getId().intValue()))
            .andExpect(jsonPath("$.nickName").value(DEFAULT_NICK_NAME))
            .andExpect(jsonPath("$.account").value(DEFAULT_ACCOUNT))
            .andExpect(jsonPath("$.password").value(DEFAULT_PASSWORD))
            .andExpect(jsonPath("$.mobile").value(DEFAULT_MOBILE))
            .andExpect(jsonPath("$.avatar").value(DEFAULT_AVATAR))
            .andExpect(jsonPath("$.slogan").value(DEFAULT_SLOGAN));
    }

    @Test
    @Transactional
    void getNonExistingRoomUser() throws Exception {
        // Get the roomUser
        restRoomUserMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putNewRoomUser() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();

        // Update the roomUser
        RoomUser updatedRoomUser = roomUserRepository.findById(roomUser.getId()).get();
        // Disconnect from session so that the updates on updatedRoomUser are not directly saved in db
        em.detach(updatedRoomUser);
        updatedRoomUser
            .nickName(UPDATED_NICK_NAME)
            .account(UPDATED_ACCOUNT)
            .password(UPDATED_PASSWORD)
            .mobile(UPDATED_MOBILE)
            .avatar(UPDATED_AVATAR)
            .slogan(UPDATED_SLOGAN);
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(updatedRoomUser);

        restRoomUserMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomUserDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isOk());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
        RoomUser testRoomUser = roomUserList.get(roomUserList.size() - 1);
        assertThat(testRoomUser.getNickName()).isEqualTo(UPDATED_NICK_NAME);
        assertThat(testRoomUser.getAccount()).isEqualTo(UPDATED_ACCOUNT);
        assertThat(testRoomUser.getPassword()).isEqualTo(UPDATED_PASSWORD);
        assertThat(testRoomUser.getMobile()).isEqualTo(UPDATED_MOBILE);
        assertThat(testRoomUser.getAvatar()).isEqualTo(UPDATED_AVATAR);
        assertThat(testRoomUser.getSlogan()).isEqualTo(UPDATED_SLOGAN);
    }

    @Test
    @Transactional
    void putNonExistingRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(
                put(ENTITY_API_URL_ID, roomUserDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(
                put(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(roomUserDTO)))
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateRoomUserWithPatch() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();

        // Update the roomUser using partial update
        RoomUser partialUpdatedRoomUser = new RoomUser();
        partialUpdatedRoomUser.setId(roomUser.getId());

        partialUpdatedRoomUser.mobile(UPDATED_MOBILE).avatar(UPDATED_AVATAR);

        restRoomUserMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomUser.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomUser))
            )
            .andExpect(status().isOk());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
        RoomUser testRoomUser = roomUserList.get(roomUserList.size() - 1);
        assertThat(testRoomUser.getNickName()).isEqualTo(DEFAULT_NICK_NAME);
        assertThat(testRoomUser.getAccount()).isEqualTo(DEFAULT_ACCOUNT);
        assertThat(testRoomUser.getPassword()).isEqualTo(DEFAULT_PASSWORD);
        assertThat(testRoomUser.getMobile()).isEqualTo(UPDATED_MOBILE);
        assertThat(testRoomUser.getAvatar()).isEqualTo(UPDATED_AVATAR);
        assertThat(testRoomUser.getSlogan()).isEqualTo(DEFAULT_SLOGAN);
    }

    @Test
    @Transactional
    void fullUpdateRoomUserWithPatch() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();

        // Update the roomUser using partial update
        RoomUser partialUpdatedRoomUser = new RoomUser();
        partialUpdatedRoomUser.setId(roomUser.getId());

        partialUpdatedRoomUser
            .nickName(UPDATED_NICK_NAME)
            .account(UPDATED_ACCOUNT)
            .password(UPDATED_PASSWORD)
            .mobile(UPDATED_MOBILE)
            .avatar(UPDATED_AVATAR)
            .slogan(UPDATED_SLOGAN);

        restRoomUserMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedRoomUser.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRoomUser))
            )
            .andExpect(status().isOk());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
        RoomUser testRoomUser = roomUserList.get(roomUserList.size() - 1);
        assertThat(testRoomUser.getNickName()).isEqualTo(UPDATED_NICK_NAME);
        assertThat(testRoomUser.getAccount()).isEqualTo(UPDATED_ACCOUNT);
        assertThat(testRoomUser.getPassword()).isEqualTo(UPDATED_PASSWORD);
        assertThat(testRoomUser.getMobile()).isEqualTo(UPDATED_MOBILE);
        assertThat(testRoomUser.getAvatar()).isEqualTo(UPDATED_AVATAR);
        assertThat(testRoomUser.getSlogan()).isEqualTo(UPDATED_SLOGAN);
    }

    @Test
    @Transactional
    void patchNonExistingRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, roomUserDTO.getId())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, count.incrementAndGet())
                    .contentType("application/merge-patch+json")
                    .content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamRoomUser() throws Exception {
        int databaseSizeBeforeUpdate = roomUserRepository.findAll().size();
        roomUser.setId(count.incrementAndGet());

        // Create the RoomUser
        RoomUserDTO roomUserDTO = roomUserMapper.toDto(roomUser);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restRoomUserMockMvc
            .perform(
                patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(roomUserDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the RoomUser in the database
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteRoomUser() throws Exception {
        // Initialize the database
        roomUserRepository.saveAndFlush(roomUser);

        int databaseSizeBeforeDelete = roomUserRepository.findAll().size();

        // Delete the roomUser
        restRoomUserMockMvc
            .perform(delete(ENTITY_API_URL_ID, roomUser.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        List<RoomUser> roomUserList = roomUserRepository.findAll();
        assertThat(roomUserList).hasSize(databaseSizeBeforeDelete - 1);
    }
}
